<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.jobs.zhlx.duss.utils.ZhlxJobUtil" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%--流程退回调用QMS接口--%>
<%
    try {
        ZhlxJobUtil jobUtil = new ZhlxJobUtil();
        String tablename = "uf_sapkhzsj";
        int formmodeid = jobUtil.getFormmodeid(tablename);
        JSONObject dataJson = new JSONObject();
        dataJson.put("BUKRS", "");
        dataJson.put("KUNNR", "1910");
        JSONObject resultJson = jobUtil.handleOKHttpRequest("FI035", dataJson);
        String code = Util.null2String(resultJson.getString("code"));
        out.print(">>>>>>WeaSyncKhBySapCron>>>>code=" + code+"<br/>");
        if ("success".equals(code)) {
            String output = Util.null2String(resultJson.getString("OUTPUT"));
            JSONObject outputJson = JSONObject.parseObject(output);
            String msgType = outputJson.getString("MSG_TYPE");
            String msgText = outputJson.getString("MSG_TEXT");
            out.print(">>>>>>WeaSyncKhBySapCron>>>>msgType=" + msgType + ",msgText=" + msgText+"<br/>");
            if ("S".equals(msgType) && "调用成功".equals(msgText)) {
                String dataReturnStr = outputJson.getString("DATA");
                JSONObject dataReturnJson = JSONObject.parseObject(dataReturnStr);
                String returnData = dataReturnJson.getString("RETURN");
                JSONArray returnList = JSONArray.parseArray(returnData);
                for (int i = 0; i < returnList.size(); i++) {
                    JSONObject paramJson = new JSONObject();
                    JSONObject returnJson = returnList.getJSONObject(i);
                    String returnHeaderStr = returnJson.getString("HEADER");
                    JSONObject returnHeaderJson = JSONObject.parseObject(returnHeaderStr);
                    paramJson.put("khzhz", returnHeaderJson.getString("KTOKD"));
                    String khbh = returnHeaderJson.getString("KUNNR");
                    paramJson.put("khbh", khbh);
                    String mc1 = returnHeaderJson.getString("NAME1");
                    String mc2 = returnHeaderJson.getString("NAME2");
                    paramJson.put("mc1", mc1);
                    paramJson.put("mc2", mc2);
                    paramJson.put("khmcq", mc1 + mc2);
                    paramJson.put("sstj1", returnHeaderJson.getString("BU_SORT1"));
                    paramJson.put("gj", returnHeaderJson.getString("LAND1"));
                    paramJson.put("dq", returnHeaderJson.getString("REGIO"));
                    paramJson.put("cs", returnHeaderJson.getString("ORT01"));
                    paramJson.put("yzbm", returnHeaderJson.getString("PSTLZ"));
                    paramJson.put("jd", returnHeaderJson.getString("STRAS"));
                    paramJson.put("yydm", returnHeaderJson.getString("SPRAS"));
                    paramJson.put("dh", returnHeaderJson.getString("TELF1"));
                    paramJson.put("yddh", returnHeaderJson.getString("TELF2"));
                    paramJson.put("cz", returnHeaderJson.getString("TELFX"));
                    paramJson.put("email", returnHeaderJson.getString("SMTP_ADDR"));
                    paramJson.put("wysbh", returnHeaderJson.getString("IDNUMBER"));
                    paramJson.put("bslx", returnHeaderJson.getString("TYPE"));
                    paramJson.put("jzdj", returnHeaderJson.getString("XBLCK"));
                    paramJson.put("myhb", returnHeaderJson.getString("VBUND"));
                    paramJson.put("gzdjsygs", returnHeaderJson.getString("SPERR"));
                    int mainid = jobUtil.getMainId(tablename, "khbh", khbh);
                    out.print(">>>>>>WeaSyncKhBySapCron>>>>mainid=" + mainid+"<br/>");
                    if (mainid <= 0) {
                        out.print(">>>>>>WeaSyncKhBySapCron>>>>插入=" + mainid+"<br/>");
                        int dataid = jobUtil.insertModelData(formmodeid, paramJson);
                        String bankStr = returnHeaderJson.getString("BANK");
                        JSONArray bankList = JSONArray.parseArray(bankStr);
                        if (!bankList.isEmpty()) {
                            for (int k = 0; k < bankList.size(); k++) {
                                JSONObject paramsJson = new JSONObject();
                                out.print(">>>>>>bankList>>>>paramsJson=" + paramsJson+"<br/>");
                                paramsJson.put("yxgjdm", bankList.getJSONObject(k).getString("BANKS"));
                                paramsJson.put("yxdm", bankList.getJSONObject(k).getString("BANKL"));
                                paramsJson.put("yxzh", bankList.getJSONObject(k).getString("BANKN"));
                                paramsJson.put("ckmx", bankList.getJSONObject(k).getString("BKREF"));
                                paramsJson.put("swift", bankList.getJSONObject(k).getString("SWIFT"));
                                paramsJson.put("kzm", bankList.getJSONObject(k).getString("BKONT"));
                                paramsJson.put("gjyxzh", bankList.getJSONObject(k).getString("IBAN"));
                                paramsJson.put("zhcyr", bankList.getJSONObject(k).getString("KOINH"));
                                paramsJson.put("mainid", dataid);
                                jobUtil.insertDetailData(tablename + "_dt1", paramsJson);
                            }
                        }
                        String bukrsStr = returnHeaderJson.getString("BUKRS");
                        JSONArray bukrsList = JSONArray.parseArray(bukrsStr);
                        if (!bukrsList.isEmpty()) {
                            for (int j = 0; j < bukrsList.size(); j++) {
                                JSONObject paramsJson = new JSONObject();
                                out.print(">>>>>>bukrsList>>>>paramsJson=" + paramsJson+"<br/>");
                                paramsJson.put("gsdm", bukrsList.getJSONObject(j).getString("BUKRS"));
                                paramsJson.put("tykm", bukrsList.getJSONObject(j).getString("AKONT"));
                                paramsJson.put("zb", bukrsList.getJSONObject(j).getString("KNRZE"));
                                paramsJson.put("pxm", bukrsList.getJSONObject(j).getString("ZUAWA"));
                                paramsJson.put("xqdbh", bukrsList.getJSONObject(j).getString("ALTKN"));
                                paramsJson.put("ygh", bukrsList.getJSONObject(j).getString("PERNR"));
                                paramsJson.put("fktj", bukrsList.getJSONObject(j).getString("ZTERM"));
                                paramsJson.put("rcz", bukrsList.getJSONObject(j).getString("TOGRU"));
                                paramsJson.put("fkfs", bukrsList.getJSONObject(j).getString("ZWELS"));
                                paramsJson.put("khyx", bukrsList.getJSONObject(j).getString("HBKID"));
                                paramsJson.put("gzdjsxgs", bukrsList.getJSONObject(j).getString("SPERR1"));
                                paramsJson.put("mainid", dataid);
                                jobUtil.insertDetailData(tablename + "_dt2", paramsJson);
                            }
                        }
                    } else {
                        out.print(">>>>>>WeaSyncKhBySapCron>>>>更新=" + mainid+"<br/>");
                        out.print(">>>>>>WeaSyncKhBySapCron>>>>tablename=" + tablename+"<br/>");
                        out.print(">>>>>>WeaSyncKhBySapCron>>>>mainid=" + mainid+"<br/>");
                        out.print(">>>>>>WeaSyncKhBySapCron>>>>paramJson=" + paramJson+"<br/>");
                        updateModelData(tablename, mainid, paramJson);
                        String bankStr = returnHeaderJson.getString("BANK");
                        out.print(">>>>>>WeaSyncKhBySapCron>>>>bankStr=" + bankStr+"<br/>");
                        JSONArray bankList = JSONArray.parseArray(bankStr);
                        if (!bankList.isEmpty()) {
                            jobUtil.deleteDetailData(tablename + "_dt1", mainid);
                            for (int k = 0; k < bankList.size(); k++) {
                                JSONObject paramsJson = new JSONObject();
                                out.print(">>>>>>bankList>>>>paramsJson=" + paramsJson+"<br/>");
                                paramsJson.put("yxgjdm", bankList.getJSONObject(k).getString("BANKS"));
                                paramsJson.put("yxdm", bankList.getJSONObject(k).getString("BANKL"));
                                paramsJson.put("yxzh", bankList.getJSONObject(k).getString("BANKN"));
                                paramsJson.put("ckmx", bankList.getJSONObject(k).getString("BKREF"));
                                paramsJson.put("wtift", bankList.getJSONObject(k).getString("SWIFT"));
                                paramsJson.put("kzm", bankList.getJSONObject(k).getString("BKONT"));
                                paramsJson.put("gjyxzh", bankList.getJSONObject(k).getString("IBAN"));
                                paramsJson.put("zhcyr", bankList.getJSONObject(k).getString("KOINH"));
                                paramsJson.put("mainid", mainid);
                                jobUtil.insertDetailData(tablename + "_dt1", paramsJson);
                            }
                        }
                        String bukrsStr = returnHeaderJson.getString("BUKRS");
                        out.print(">>>>>>WeaSyncKhBySapCron>>>>bukrsStr=" + bukrsStr+"<br/>");
                        JSONArray bukrsList = JSONArray.parseArray(bukrsStr);
                        if (!bukrsList.isEmpty()) {
                            jobUtil.deleteDetailData(tablename + "_dt2", mainid);
                            for (int j = 0; j < bukrsList.size(); j++) {
                                JSONObject paramsJson = new JSONObject();
                                out.print(">>>>>>bukrsList>>>>paramsJson=" + paramsJson+"<br/>");
                                paramsJson.put("gsdm", bukrsList.getJSONObject(j).getString("BUKRS"));
                                paramsJson.put("tykm", bukrsList.getJSONObject(j).getString("AKONT"));
                                paramsJson.put("zb", bukrsList.getJSONObject(j).getString("KNRZE"));
                                paramsJson.put("pxm", bukrsList.getJSONObject(j).getString("ZUAWA"));
                                paramsJson.put("xqdbh", bukrsList.getJSONObject(j).getString("ALTKN"));
                                paramsJson.put("ygh", bukrsList.getJSONObject(j).getString("PERNR"));
                                paramsJson.put("fktj", bukrsList.getJSONObject(j).getString("ZTERM"));
                                paramsJson.put("rcz", bukrsList.getJSONObject(j).getString("TOGRU"));
                                paramsJson.put("fkfs", bukrsList.getJSONObject(j).getString("ZWELS"));
                                paramsJson.put("khyx", bukrsList.getJSONObject(j).getString("HBKID"));
                                paramsJson.put("gzdjsxgs", bukrsList.getJSONObject(j).getString("SPERR1"));
                                paramsJson.put("mainid", mainid);
                                jobUtil.insertDetailData(tablename + "_dt2", paramsJson);
                            }
                        }
                    }


                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

%>

<%!
    /**
     * 更新数据
     *
     * @param tablename 表名
     * @param paramJson 数据集合
     * @param dataid    更新用唯一标识
     * @return
     */
    public boolean updateModelData(String tablename, int dataid, JSONObject paramJson) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog(">>>>>updateModelData>>>>tablename=" + tablename);
        baseBean.writeLog(">>>>>updateModelData>>>>dataid=" + dataid);
        baseBean.writeLog(">>>>>updateModelData>>>>dataid=" + dataid);
        RecordSet rs = new RecordSet();
        String updatesql = "update " + tablename + " set  ";
        String valuesStr = "";
        Map<String, Object> map = JSONObject.toJavaObject(paramJson, Map.class);
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (valuesStr.equals("")) {
                valuesStr = key + "='" + map.get(key) + "'";
            } else {
                valuesStr += "," + key + "='" + map.get(key) + "'";
            }
        }
        updatesql += valuesStr + " where id='" + dataid + "'";
        baseBean.writeLog(">>>>>updateModelData>>>>updatesql=" + updatesql);
        return rs.execute(updatesql);
    }
%>
